package com.zhuiYun.project.api.seckill.service.impl;
import com.zhuiYun.project.api.commodity.entity.Commodity;
import com.zhuiYun.project.api.seckill.entity.Seckill;
import com.zhuiYun.project.api.seckill.mapper.SeckillMapper;
import com.zhuiYun.project.api.seckill.model.SeckillModel;
import com.zhuiYun.project.api.seckill.service.SeckillService;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonConstants;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.BeanUtils;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.RedisUtil;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SeckillServiceImpl
 * @Description TODO
 * Author 高
 * @Date 2023/4/11 10:15
 * @Version 1.0
 **/
@Service
public class SeckillServiceImpl implements SeckillService {
    @Autowired
    SeckillMapper seckillMapper;

    /*
     * @Author 高哲
     * @Description 查询秒杀信息
     * @Date 2023/4/12 10:11
     * @Return
     * @Param
     */
    @Override
    public List<Seckill> getSeckillByPage(Seckill seckill) {
        return seckillMapper.selectSeckillAndCommodity();
    }
    /*
     * @Author 高哲
     * @Description 添加秒杀
     * @Date 2023/4/12 10:50
     * @Return
     * @Param
     */
    @Override
    public int SeckillAdd(Seckill seckill) throws CommonException {
        seckill.setDelFlag(0);
        int i = 0;
        i = seckillMapper.insertUseGeneratedKeys(seckill);
        long now = System.currentTimeMillis();
        Date seckillEndDate = seckill.getSeckillEndDate();
        long overTime = (seckillEndDate.getTime() - now) /1000;
        RedisUtil.set(CommonConstants.SECKILL + seckill.getId(), seckill, overTime);
        return i;
    }
    /*
     * @Author 高哲
     * @Description 修改秒杀
     * @Date 2023/4/12 15:17
     * @Return
     * @Param
     */
    @Override
    public int SeckillUpdate(Seckill seckill) throws CommonException {
        seckill.setDelFlag(0);
        int i = 0;
        i = seckillMapper.updateByPrimaryKeySelective(seckill);
        return i;
    }
    /*
     * @Author 高哲
     * @Description 删除秒杀
     * @Date 2023/4/12 15:10
     * @Return
     * @Param
     */
    @Override
    public CommonResult delSeckillById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = seckillMapper.deleteByPrimaryKey(id);
            // 判断是否操作成功
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

}
